package com.dartsstats;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.dartsstats.R;
import com.dartsstats.R.id;
import com.dartsstats.R.layout;
import com.dartsstats.R.menu;
import com.dartsstats.datasource.GameDataSource;
import com.dartsstats.datasource.GameLineupDataSource;
import com.dartsstats.datasource.MatchDataSource;
import com.dartsstats.datasource.PlayerDataSource;
import com.dartsstats.datasource.TeamDataSource;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class SetLineupsActivity extends Activity {

	private Team homeTeam;
	private Team awayTeam;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		if(extras != null) {
			long homeTeamId = extras.getLong("HOMETEAM_ID");
			homeTeam = new TeamDataSource(this).getTeamByID((int)homeTeamId);
			long awayTeamId = extras.getLong("AWAYTEAM_ID");
			awayTeam = new TeamDataSource(this).getTeamByID((int)awayTeamId);
		}
		setContentView(R.layout.activity_set_lineups);
		createView();
	}
	
	private void createView() {
		TextView awayTeamTextView = (TextView) findViewById(R.id.awayTeamName);
		awayTeamTextView.setText(awayTeam.getName());
		TextView homeTeamTextView = (TextView) findViewById(R.id.homeTeamName);
		homeTeamTextView.setText(homeTeam.getName());
		
		long awayTeamId = awayTeam.getId();
		List<Player> awayPlayers = new PlayerDataSource(this).getAllPlayersForTeam((int)awayTeamId);
		setupAwayTeamSpinners(awayPlayers);
		long homeTeamId = homeTeam.getId();
		List<Player> homePlayers = new PlayerDataSource(this).getAllPlayersForTeam((int)homeTeamId);
		setupHomeTeamSpinners(homePlayers);
		Spinner gameNumberSpinner = (Spinner) findViewById(R.id.gameNumberSpinner);
		Integer[] gameNumberArray = new Integer[3];
		gameNumberArray[0] = 1;
		gameNumberArray[1] = 2;
		gameNumberArray[2] = 3;
		ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
				android.R.layout.simple_spinner_item, gameNumberArray);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		gameNumberSpinner.setAdapter(adapter);
	}
	
	private void setupAwayTeamSpinners(List<Player> awayPlayers) {
		Spinner spinner = (Spinner) findViewById(R.id.awayPlayer1);
		applyAdapterToSpinner(awayPlayers, spinner, 0);
		spinner = (Spinner) findViewById(R.id.awayPlayer2);
		applyAdapterToSpinner(awayPlayers, spinner, 1);
		spinner = (Spinner) findViewById(R.id.awayPlayer3);
		applyAdapterToSpinner(awayPlayers, spinner, 2);
		spinner = (Spinner) findViewById(R.id.awayPlayer4);
		applyAdapterToSpinner(awayPlayers, spinner, 3);
		spinner = (Spinner) findViewById(R.id.awayPlayer5);
		applyAdapterToSpinner(awayPlayers, spinner, 4);
		spinner = (Spinner) findViewById(R.id.awayPlayer6);
		applyAdapterToSpinner(awayPlayers, spinner, 5);
		spinner = (Spinner) findViewById(R.id.awayPlayer7);
		applyAdapterToSpinner(awayPlayers, spinner, 6);
		spinner = (Spinner) findViewById(R.id.awayPlayer8);
		applyAdapterToSpinner(awayPlayers, spinner, 7);
		spinner = (Spinner) findViewById(R.id.awayPlayer9);
		applyAdapterToSpinner(awayPlayers, spinner, 8);
	}
	
	private void setupHomeTeamSpinners(List<Player> homePlayers) {
		Spinner spinner = (Spinner) findViewById(R.id.homePlayer1);
		applyAdapterToSpinner(homePlayers, spinner, 0);
		spinner = (Spinner) findViewById(R.id.homePlayer2);
		applyAdapterToSpinner(homePlayers, spinner, 1);
		spinner = (Spinner) findViewById(R.id.homePlayer3);
		applyAdapterToSpinner(homePlayers, spinner, 2);
		spinner = (Spinner) findViewById(R.id.homePlayer4);
		applyAdapterToSpinner(homePlayers, spinner, 3);
		spinner = (Spinner) findViewById(R.id.homePlayer5);
		applyAdapterToSpinner(homePlayers, spinner, 4);
		spinner = (Spinner) findViewById(R.id.homePlayer6);
		applyAdapterToSpinner(homePlayers, spinner, 5);
		spinner = (Spinner) findViewById(R.id.homePlayer7);
		applyAdapterToSpinner(homePlayers, spinner, 6);
		spinner = (Spinner) findViewById(R.id.homePlayer8);
		applyAdapterToSpinner(homePlayers, spinner, 7);
		spinner = (Spinner) findViewById(R.id.homePlayer9);
		applyAdapterToSpinner(homePlayers, spinner, 8);
	}

	private void applyAdapterToSpinner(List<Player> awayPlayers, Spinner spinner, int position) {
		ArrayAdapter<Player> adapter = new ArrayAdapter<Player>(this,
				android.R.layout.simple_spinner_item, awayPlayers);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setSelection(position);
	}
	
	public void submitLineups(View view) {
		Intent intent = new Intent(this, GameActivity.class);
		List<Player> awayPlayers = new ArrayList<Player>();
		buildAndCheckAwayPlayers(awayPlayers);
		List<Player> homePlayers = new ArrayList<Player>();
		buildAndCheckHomePlayers(homePlayers);
		int gameNumber = (Integer)((Spinner)findViewById(R.id.gameNumberSpinner)).getSelectedItem();
		if(gameNumber == 1) {
			MatchDataSource matchDataSource = new MatchDataSource(this);
			Calendar calendar = Calendar.getInstance();
			Date date = calendar.getTime();
			int seasonHalf;
			if(date.getMonth()<6) {
				seasonHalf = 2;
			} else {
				seasonHalf = 1;
			}
			Match match = matchDataSource.createMatch(awayTeam.getId(), homeTeam.getId(), date, seasonHalf);
			
			
			GameDataSource gameDataSource = new GameDataSource(this);
			Game game = gameDataSource.createGame(match.getId(), gameNumber, 0, 0);
			
			addTeamToDB(awayPlayers, game.getId());
			addTeamToDB(homePlayers, game.getId());
			intent.putExtra("MATCH_ID", match.getId());
			intent.putExtra("GAME_ID", game.getId());
			startActivity(intent);
		}
	}

	private void addTeamToDB(List<Player> players, long gameId) {
		GameLineupDataSource gameLineupDataSource = new GameLineupDataSource(this);
		for(int i = 0; i<players.size();i++) {
			Player player = players.get(i);
			gameLineupDataSource.createGameLineup(gameId, player.getId(), i+1, 1, -1);
		}
	}

	private void buildAndCheckAwayPlayers(List<Player> awayPlayers) {
		Player player = ((Player) ((Spinner) findViewById(R.id.awayPlayer1)).getSelectedItem());
		awayPlayers.add(player);
		player = ((Player) ((Spinner) findViewById(R.id.awayPlayer2)).getSelectedItem());
		addAndCheckPlayer(awayPlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.awayPlayer3)).getSelectedItem());
		addAndCheckPlayer(awayPlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.awayPlayer4)).getSelectedItem());
		addAndCheckPlayer(awayPlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.awayPlayer5)).getSelectedItem());
		addAndCheckPlayer(awayPlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.awayPlayer6)).getSelectedItem());
		addAndCheckPlayer(awayPlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.awayPlayer7)).getSelectedItem());
		addAndCheckPlayer(awayPlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.awayPlayer8)).getSelectedItem());
		addAndCheckPlayer(awayPlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.awayPlayer9)).getSelectedItem());
		addAndCheckPlayer(awayPlayers, player);
	}
	private void buildAndCheckHomePlayers(List<Player> homePlayers) {
		Player player = ((Player) ((Spinner) findViewById(R.id.homePlayer1)).getSelectedItem());
		homePlayers.add(player);
		player = ((Player) ((Spinner) findViewById(R.id.homePlayer2)).getSelectedItem());
		addAndCheckPlayer(homePlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.homePlayer3)).getSelectedItem());
		addAndCheckPlayer(homePlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.homePlayer4)).getSelectedItem());
		addAndCheckPlayer(homePlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.homePlayer5)).getSelectedItem());
		addAndCheckPlayer(homePlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.homePlayer6)).getSelectedItem());
		addAndCheckPlayer(homePlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.homePlayer7)).getSelectedItem());
		addAndCheckPlayer(homePlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.homePlayer8)).getSelectedItem());
		addAndCheckPlayer(homePlayers, player);
		player = ((Player) ((Spinner) findViewById(R.id.homePlayer9)).getSelectedItem());
		addAndCheckPlayer(homePlayers, player);
	}

	private void addAndCheckPlayer(List<Player> awayPlayers, Player player) {
		if (!awayPlayers.contains(player)) {
			awayPlayers.add(player);
		} else {
			AlertDialog.Builder alert = new AlertDialog.Builder(this);

			alert.setTitle("Invalid lineup");
			alert.setMessage("Player assigned to multiple spots");
			alert.setPositiveButton("Ok",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
						}
					});
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_set_lineups, menu);
		return true;
	}

}
